// 定义和用户目标相关的接口
const Router = require('koa-router')
const router = new Router()
const {
    getTargetData,
    changeTargetWeight,
    writeTargetData,
    changeTargetIntake,
    changeTargetConsume
} = require('../controllers/index.js')
const { verify } = require('../utils/jwt.js')

router.prefix('/target')

// 修改用户的目标体重
router.post('/targetWeight', verify(), async (ctx, next) => {
    const { targetWeight } = ctx.request.body
    const id = ctx.user_id
    // console.log(targetWeight, id);
    const res = await changeTargetWeight(targetWeight, id)
    // console.log(res);
    try {
        if (res.affectedRows) {
            ctx.body = {
                code: 200,
                flag: true,
                msg: '修改成功',
            }
        } else {
            ctx.body = {
                code: 200,
                flag: false,
                msg: '修改失败',
            }
        }
    } catch (error) {
        ctx.body = {
            code: 500,
            data: error,
            msg: '服务器异常',
        }
    }
})

//获取用户的目标体重、目标摄入和目标消耗
router.get('/getTargetData', verify(), async (ctx, next) => {
    const user_id = ctx.user_id
    const res = await getTargetData(user_id)
    // console.log(res);
    try {
        if (res.length) {
            ctx.body = {
                code: 200,
                data: res,
                msg: '获取成功',
            }
        } else {
            ctx.body = {
                code: 200,
                data: [],
                msg: '暂无数据',
            }
        }
    } catch (error) {
        ctx.body = {
            code: 500,
            data: error,
            msg: '服务器异常',
        }
    }
})

// 写入用户的目标体重、目标摄入和目标消耗
router.post('/writeTargetData', verify(), async (ctx, next) => {
    const { targetWeight, targetIntake, targetConsume } = ctx.request.body
    const user_id = ctx.user_id
    // console.log(user_id);
    const res = await writeTargetData(targetWeight, targetIntake, targetConsume, user_id)
    console.log(res);
    try {
        if (res.affectedRows) {
            ctx.body = {
                code: 200,
                flag: true,
                msg: '写入成功',
            }
        } else {
            ctx.body = {
                code: 200,
                flag: false,
                msg: '写入失败',
            }
        }
    } catch (error) {
        ctx.body = {
            code: 500,
            data: error,
            msg: '服务器异常',
        }
    }
})

// 修改目标摄入热量
router.post('/targetIntake', verify(), async (ctx, next) => {
    const { targetIntake } = ctx.request.body
    const id = ctx.user_id
    // console.log(targetIntake, id);
    const res = await changeTargetIntake(targetIntake, id)
    // console.log(res);
    try {
        if (res.affectedRows) {
            ctx.body = {
                code: 200,
                flag: true,
                msg: '修改成功',
            }
        } else {
            ctx.body = {
                code: 200,
                flag: false,
                msg: '修改失败',
            }
        }
    } catch (error) {
        ctx.body = {
            code: 500,
            data: error,
            msg: '服务器异常',
        }
    }
})

// 修改目标消耗热量
router.post('/targetConsume', verify(), async (ctx, next) => {
    const { targetConsume } = ctx.request.body
    const id = ctx.user_id
    console.log(targetConsume, id);
    const res = await changeTargetConsume(targetConsume, id)
    console.log(res);
    try {
        if (res.affectedRows) {
            ctx.body = {
                code: 200,
                flag: true,
                msg: '修改成功',
            }
        } else {
            ctx.body = {
                code: 200,
                flag: false,
                msg: '修改失败',
            }
        }
    } catch (error) {
        ctx.body = {
            code: 500,
            data: error,
            msg: '服务器异常',
        }
    }
})

module.exports = {
    targetRouter: router
}